package com.xinqi.common.green.validator;

import com.alibaba.fastjson.JSON;
import com.xinqi.common.green.annotation.Text;
import com.xinqi.common.green.utils.SensitiveWordUtils;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Objects;

/**
 * @Author：yyzhen
 * @Package：com.xinqi.common.green.validator
 * @Project：xinqiclass-cloud-plus
 * @name：TextValidator
 * @Date：2023/4/21 13:56
 * @Filename：TextValidator
 */
@Slf4j
public class TextValidator implements ConstraintValidator<Text, Object>, Annotation {

    @Override
    public Class<? extends Annotation> annotationType() {
        return null;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
        if (Objects.isNull(value)) {
            return Boolean.TRUE;
        }
        if (value instanceof String) {
            return SensitiveWordUtils.checkSensitiveWord((String) value);
            //return SyncScan.getTextResult(GreenTextUtils.textScan((String) value));
        }
        if (value instanceof List) {
            return SensitiveWordUtils.checkSensitiveWord(JSON.toJSONString(value));
            //return SyncScan.getTextListResult(GreenTextUtils.textScan((List<String>) value));
        }

        return Boolean.TRUE;
    }
}
